package com.bing.my.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bing.my.entity.Bill;
import com.bing.my.entity.GooseBill;
import com.bing.my.entity.Member;
import com.bing.my.entity.vo.VoPay;
import com.bing.my.mapper.BillMapper;
import com.bing.my.service.BillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.my.service.GooseBillService;
import com.bing.my.service.MemberService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 账单数据 服务实现类
 * </p>
 *
 * @author bing
 * @since 2021-10-26
 */
@Service
@Log4j2
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements BillService {

    @Resource
    private BillMapper billMapper;
    @Resource
    private MemberService memberService;
    @Resource
    private GooseBillService gooseBillService;

    @Override
    public int insert(Bill bill) {
        return billMapper.insert(bill);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean pay(String id, Bill bill, ArrayList<GooseBill> gooseBill) {
        try {
            // 根据id查询会员金额
            Member member = memberService.selectById(id);
            BigDecimal balance = member.getBalance();
            // 手动设置id
            String billId = bill.getBillId();
            for (GooseBill g : gooseBill) {
                // 插入gooseBill数据
                g.setBillId(billId);
                gooseBillService.insert(g);
            }
            // 插入bill账单
            billMapper.insert(bill);
            // 扣除余额
            BigDecimal realMoney = bill.getRealMoney();
            BigDecimal subtract = balance.subtract(realMoney);
            if (subtract.compareTo(BigDecimal.ZERO) <= 0) {
                log.error("余额不足,支付失败----------");
                throw new Exception("余额不足");
            } else {
                member.setBalance(subtract);
                memberService.update(member);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("支付失败-------");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }

    @Override
    public List<Bill> inquireBill(Date star, Date end) {
        QueryWrapper<Bill> wrapper = new QueryWrapper<>();
        List<Bill> bills = billMapper.selectList(wrapper.between("gmt_create", star, end));
        return bills;
    }

    @Override
    public List<VoPay> inquirePayData(String star, String end) {
        String pattern = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        Date starDate = null;
        Date endDate = null;
        try {
            starDate = dateFormat.parse(star);
            endDate = dateFormat.parse(end);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 查出bill数据
        List<Bill> bills = this.inquireBill(starDate, endDate);
        ArrayList<VoPay> voPay = new ArrayList<VoPay>();
        // 根据bill表billId查询出gooseBill数据
        for (Bill b : bills) {
            VoPay pay = new VoPay();
            pay.setBill(b);
            List<GooseBill> gooseBills = gooseBillService.inquireGooseBill(b.getBillId());
            pay.setGooseBill((ArrayList<GooseBill>) gooseBills);
            voPay.add(pay);
        }
        return voPay;
    }

}
